/*
* AirDoorController.java
* Created on  2015/7/27 15:07
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/27       longshihui    初始版本
*
*/

package com.infitecs.eyas.tools;

import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.tool.AirDoor;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/tools/airdoor")
public class AirDoorController {

    @Autowired
    private ToolExpressionService toolExpressionService;

    @Autowired
    private SessionService sessionService;


    @RequestMapping("")
    public String getPage(){
        return "tools/airdoor";
    }

    @RequestMapping(value = "/calE15",method = RequestMethod.GET)
    @ResponseBody
    public Double calE15() {
        Map<String, Double> paramMap = toolExpressionService.getParamMap();
        Double E9 = 0.0;
        paramMap.put("TE9",E9);
        return toolExpressionService.calculate("TE15");
    }


    @RequestMapping(value = "/updateParam", method = RequestMethod.POST)
    @ResponseBody
    public void updateParam(AirDoor airDoor){
        toolExpressionService.updateToolsParam(airDoor);
    }


    @RequestMapping(value = "/clean", method = RequestMethod.POST)
    @ResponseBody
    public void clean(){
        toolExpressionService.clearMap();
        toolExpressionService.clearResultMap();
    }

    @RequestMapping(value = "/cal", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Double> calAll() {
        Map<String, Double> map = new HashMap<>();
        toolExpressionService.calculate("TE8");
        toolExpressionService.calculate("TE14");
        toolExpressionService.calculate("TE16");
        toolExpressionService.calculate("TE21");
        toolExpressionService.calculate("TE222");
        toolExpressionService.calculate("TE223");
        toolExpressionService.calculate("TE224");
        toolExpressionService.calculate("TE225");
        toolExpressionService.calculate("TE226");
        toolExpressionService.calculate("TE227");
        toolExpressionService.calculate("TE228");
        toolExpressionService.calculate("TE229");
        toolExpressionService.calculate("TE220");
        toolExpressionService.calculate("TE232");
        map.put("TE232", toolExpressionService.calculate("TE232"));
        map.put("e19", toolExpressionService.calculate("TE19"));
        map.put("e21", toolExpressionService.calculate("TE21"));
        map.put("e233", toolExpressionService.calculate("TE233"));
        map.put("e234", toolExpressionService.calculate("TE234"));
        toolExpressionService.getResult().put("p", map.get("e19"));
        toolExpressionService.getResult().put("z", map.get("e233"));
        toolExpressionService.getResult().put("lost", map.get("e233") * map.get("e19"));
        return map;
    }

    @RequestMapping(value = "airDoorCal", method = RequestMethod.POST)
      @ResponseBody
      public AjaxResponse airDoorCal(@RequestBody Map<String, Double> paramMap) {
          try {
              return  toolExpressionService.airDoorCal(paramMap);
          } catch (Exception e) {
              e.printStackTrace();
              if (e instanceof ValidateErrorException) {
                  return new AjaxResponse(ReturnState.WARNING, e.getMessage());
              }
              return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
          }
      }
}
